package iscript.uiFramework.components.core 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	public class DragManager 
	{
		
		public function DragManager() 
		{
			
		}
		
		static private var _currentTarget:DisplayObject;
		static public function add(target:DisplayObject,area:Rectangle=null):void {
			 target.addEventListener(MouseEvent.MOUSE_DOWN, _startDragLogic);
			 target.addEventListener(Event.REMOVED_FROM_STAGE, _removeFormStageLogic);
		}
		
		static public function remove(target:DisplayObject):void {
			 target.removeEventListener(Event.REMOVED_FROM_STAGE, _removeFormStageLogic);
			 target.removeEventListener(MouseEvent.MOUSE_DOWN, _startDragLogic);
			}
			
		static private function _removeFormStageLogic(e:Event):void 
		{
			e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, _removeFormStageLogic);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, _startDragLogic);
		}
		
		static private function _startDragLogic(e:MouseEvent):void 
		{
			_currentTarget = DisplayObject(e.currentTarget);
			Sprite(e.currentTarget).stage.addEventListener(MouseEvent.MOUSE_UP, _stopDragLogic);
			Sprite(e.currentTarget).startDrag();
		}
		
		static private function _stopDragLogic(e:MouseEvent):void 
		{
			if (_currentTarget != null) {
					Sprite(_currentTarget).stopDrag();
					_currentTarget = null;
				}
			
			
		}
		
		
		
		
		
	}

}